package com.cloud.system.controller;

import com.github.pagehelper.PageHelper;
import com.cloud.common.constants.ExcelPoiConstants;
import com.cloud.common.domain.Result;
import com.cloud.controller.BaseController;
import com.cloud.enums.BusinessType;
import com.cloud.common.enums.CheckType;
import com.cloud.logs.Logs;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.system.entity.SysDictType;
import com.cloud.system.service.ISysDictTypeService;
import com.cloud.utils.ExcelPoiUtils;
import com.cloud.utils.SecurityUtils;
import com.cloud.utils.ServletUtils;
import com.cloud.utils.ToolUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典类型管理
 *
 * @author zx, yzj
 */
@Tag(name = "字典类型")
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController {

    @Autowired
    private ISysDictTypeService dictTypeService;

    @Operation(summary = "查询字典列表")
    @RequestPermissions("system:dict:list")
    @GetMapping("/list")
    public Result list(SysDictType dictType) {
        startPage();
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        return getDataTable(list);
    }

    @Operation(summary = "导出字典列表")
    @Logs(title = "导出字典类型", businessType = BusinessType.EXPORT)
    @RequestPermissions("system:dict:export")
    @GetMapping("/export")
    public void export(SysDictType dictType) {
        PageHelper.startPage(ExcelPoiConstants.startNum, ExcelPoiConstants.maxSize);
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        String titleName = "字典类型列表";
        ExcelPoiUtils.exportExcel(list, titleName, "sys_dict_type", SysDictType.class,
                titleName + ".xls", ServletUtils.getResponse());
    }

    @Operation(summary = "查询字典详细")
    @Parameter(name = "dictId", description = "字典类型ID")
    @RequestPermissions("system:dict:query")
    @GetMapping("/getInfo")
    public Result getInfo(Long dictId) {
        return Result.success("查询成功", dictTypeService.selectDictTypeById(dictId));
    }

    @Operation(summary = "新增字典类型")
    @Logs(title = "新增字典类型", businessType = BusinessType.INSERT)
    @RequestPermissions("system:dict:add")
    @PostMapping("/insert")
    public Result insert(@RequestBody SysDictType dict) {
        if (CheckType.NOT_UNIQUE.getCode().equals(dictTypeService.checkDictTypeUnique(dict))) {
            return Result.error("新增字典[" + dict.getDictName() + "]失败,字典类型已存在");
        }
        dict.setCreateBy(SecurityUtils.getUserName());
        return toResult(dictTypeService.insertDictType(dict));
    }

    @Operation(summary = "修改字典类型")
    @Logs(title = "修改字典类型", businessType = BusinessType.UPDATE)
    @RequestPermissions("system:dict:edit")
    @PostMapping("/update")
    public Result update(@RequestBody SysDictType dict) {
        if (CheckType.NOT_UNIQUE.getCode().equals(dictTypeService.checkDictTypeUnique(dict))) {
            return Result.error("修改字典[" + dict.getDictName() + "]失败,字典类型已存在");
        }
        return toResult(dictTypeService.updateDictType(dict));
    }

    @Operation(summary = "删除字典类型")
    @Parameter(name = "dictIds", description = "字典主建[多个用,隔开]")
    @Logs(title = "删除字典类型", businessType = BusinessType.DELETE)
    @RequestPermissions("system:dict:remove")
    @PostMapping("/remove")
    public Result remove(String dictIds) {
        List<Long> list = ToolUtils.setIdsToList(dictIds);
        return toResult(dictTypeService.deleteDictTypeByIds(list));
    }

    @Operation(summary = "刷新字典缓存")
    @Logs(title = "刷新字典类型", businessType = BusinessType.CLEAN)
    @RequestPermissions("system:dict:remove")
    @GetMapping("/clearCache")
    public Result clearCache() {
        dictTypeService.clearCache();
        return Result.success("操作成功");
    }

    @Operation(summary = "查询字典选择框列表")
    @GetMapping("/optionSelect")
    public Result optionSelect() {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return Result.success("查询成功", dictTypes);
    }

}
